local hua__chaijun_xianhua = fk.CreateSkill {
  name = "hua__chaijun_xianhua",
  tags = {}
}

Fk:loadTranslationTable {
  ["hua__chaijun_xianhua"] = "献花",
  [":hua__chaijun_xianhua"] = [[<a href='#SpellSkillDesc'>施咒技</a>，当游戏开始时，你体力值变化和死亡时，
  你向一名角色献花，并令其获得1枚【花】标记。拥有【花】的角色每回合出牌时间共为60-x秒，其回合结束后，
  将【花】传递给一名其他角色。（x为其收到【花】的次数且不高于30）]],
  ['@hua__chaijun_xianhua'] = '花',
  ['#hua__chaijun_xianhua'] = '请选择一名角色，你对其赠【花】',
}


local spec = {
  anim_type = "special",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:getMark("hua__chaijun_xianhua_p") ~= 0
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local target_p = room:askToChoosePlayers(player, {
      targets = room.alive_players,
      max_num = 1,
      min_num = 1,
      prompt = "#hua__chaijun_xianhua",
      skill_name = "hua__chaijun_xianhua"
    })
    event:setCostData(self, { target = target_p })
    return #target_p > 0
  end,
  on_use = function(self, event, target, player, data)
    local target_p = event:getCostData(self).target[1]
    local room = player.room
    room:doIndicate(player, { target_p })
    player:chat("$@Flower:" .. target_p.id)
    if target_p:getMark("@hua__chaijun_xianhua") < 30 then
      room:addPlayerMark(target_p, "@hua__chaijun_xianhua", 1)
    end
    room:setPlayerMark(target_p, "hua__chaijun_xianhua", tostring(60 - target_p:getMark("@hua__chaijun_xianhua")))
  end
}

hua__chaijun_xianhua:addEffect(fk.GameStart, {
  anim_type = "special",
  can_trigger = function(self, event, target, player, data)
    return player:getMark("hua__chaijun_xianhua_p") ~= 0
  end,
  on_cost = spec.on_cost,
  on_use = spec.on_use,
})

hua__chaijun_xianhua:addEffect(fk.BeforeRequestAsk, {
  can_refresh = function(self, event, target, player, data)
    if data == nil then return end
    return ((data.players and table.contains(data.players, player)) or
          (data.focus_players and table.contains(data.focus_players, player)))
        and player:getMark("hua__chaijun_xianhua") ~= 0
  end,
  on_refresh = function(self, event, target, player, data)
    if data == nil then return end
    print(data.timeout)
    data.timeout = tonumber(player:getMark("hua__chaijun_xianhua"))
    print(data.timeout)
    player.room:setPlayerMark(player, "hua__chaijun_xianhua_now", tostring(os.time()))
  end
})

hua__chaijun_xianhua:addEffect(fk.AfterRequestAsk, {
  can_refresh = function(self, event, target, player, data)
    if data == nil then return end
    return ((data.players and table.contains(data.players, player)) or
          (data.focus_players and table.contains(data.focus_players, player)))
        and player:getMark("hua__chaijun_xianhua") ~= 0 and player:getMark("hua__chaijun_xianhua_now") ~= 0
  end,
  on_refresh = function(self, event, target, player, data)
    if data == nil then return end
    local num = os.time() - tonumber(player:getMark("hua__chaijun_xianhua_now") or 0)
    player.room:setPlayerMark(player, "hua__chaijun_xianhua",
      tostring(tonumber(player:getMark("hua__chaijun_xianhua")) - num < 0 and 0 or
        tonumber(player:getMark("hua__chaijun_xianhua")) - num))
    player.room:setPlayerMark(player, "hua__chaijun_xianhua_now", 0)
  end
})

hua__chaijun_xianhua:addEffect(fk.HpChanged, {
  anim_type = spec.anim_type,
  can_trigger = spec.can_trigger,
  on_cost = spec.on_cost,
  on_use = spec.on_use,
})

hua__chaijun_xianhua:addEffect(fk.Death, {
  anim_type = spec.anim_type,
  can_trigger = spec.can_trigger,
  on_cost = spec.on_cost,
  on_use = spec.on_use,
})

hua__chaijun_xianhua:addEffect(fk.TurnStart, {
  can_refresh = function(self, event, target, player, data)
    return player:getMark("@hua__chaijun_xianhua") ~= 0
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, "hua__chaijun_xianhua", tostring(60 - player:getMark("@hua__chaijun_xianhua")))
  end
})
hua__chaijun_xianhua:addEffect(fk.TurnEnd, {
  can_trigger = function(self, event, target, player, data)
    return target == player and player:getMark("@hua__chaijun_xianhua") ~= 0
  end,
  on_cost = spec.on_cost,
  on_use = function(self, event, target, player, data)
    local target_p = event:getCostData(self).target[1]
    local room = player.room
    room:doIndicate(player, { target_p })
    player:chat("$@Flower:" .. target_p.id)
    if target_p:getMark("@hua__chaijun_xianhua") < 30 then
      local num = math.min(target_p:getMark("@hua__chaijun_xianhua") + player:getMark("@hua__chaijun_xianhua"), 30)
      room:setPlayerMark(target_p, "@hua__chaijun_xianhua", num)
    end
    room:setPlayerMark(target_p, "hua__chaijun_xianhua", tostring(60 - target_p:getMark("@hua__chaijun_xianhua")))
    room:setPlayerMark(player, "hua__chaijun_xianhua", 0)
    room:setPlayerMark(player, "@hua__chaijun_xianhua", 0)
  end
})

hua__chaijun_xianhua:addAcquireEffect(function(self, player, is_start)
  player.room:setPlayerMark(player, "hua__chaijun_xianhua_p", 1)
end)

return hua__chaijun_xianhua
